package api

import (
	"admin100/app/dao"
	"admin100/app/model"
	"admin100/app/service"
	"admin100/library/define"
	"admin100/library/define/defErr"
	"admin100/library/define/defLang"
	"admin100/library/response"
	"fmt"
	"strings"

	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
)

var ResourceMgr = &resourceMgrApi{}

type resourceMgrApi struct {
}

// @summary 获取资源列表
// @tags 设置
// @produce json
// @router  /resource/list [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *resourceMgrApi) List(r *ghttp.Request) {
	response.JsonSuc(r, g.Map{"rows": service.Resource.List()})
}

// @summary 编辑添加资源
// @tags 设置
// @produce json
// @param   id formData  int false "ID"
// @param   name formData  string true "资源名称"
// @param   parentId formData  string true "父资源id"
// @param   urlFor formData  string true "地址"
// @router  /resource/edit [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *resourceMgrApi) Edit(r *ghttp.Request) {
	var req model.ResourceCommonReq
	if err := r.ParseForm(&req); err != nil {
		response.JsonExitWithMsg(r, define.Fail, err.Error())
	}

	if req.ParentId > 0 {
		p := service.Resource.FetchResourceOne(req.ParentId)
		if p == nil {
			response.JsonFail(r, define.ParentNotExist)
		}
		// 父节点只能是根节点
		if p.Id == req.Id || p.ParentId > 0 {
			response.JsonFail(r, define.ParentIllegal)
		}
	}

	if strings.Contains(req.UrlFor, ".*") {

	} else {
		response.JsonExitWithMsg(r, define.Fail, req.UrlFor+defLang.GotMsg(define.ParseCtrlFail))
	}

	var id int64 = 0
	if req.Id == 0 {
		res, err := dao.MyadminResource.Save(&req)
		if err != nil {
			defErr.Panic(define.DataOptFail)
		}
		id, err = res.LastInsertId()
		if err != nil {
			defErr.Panic(define.DataOptFail, err.Error())
		}
	} else {
		if req.ParentId > 0 && !service.Resource.CanParent(req.Id, req.ParentId) {
			defErr.Panic(define.ParentIllegal2)
		}
		// var resource = model.ResourcePrototype{}
		// if err := gconv.Struct(req, &resource); err != nil {
		// 	defErr.Panic(define.DataOptFail, err.Error())
		// }
		res, err := dao.MyadminResource.Save(req)
		if err != nil {
			defErr.Panic(define.DataOptFail, err.Error())
		}
		id, err = res.RowsAffected()
		if err != nil {
			defErr.Panic(define.DataOptFail, err.Error())
		}
	}

	fmt.Print(id)
	response.JsonSuc(r, id)
}

// @summary 删除资源
// @tags 设置
// @produce json
// @param   id formData  int false "ID"
// @router  /resource/del [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *resourceMgrApi) Del(r *ghttp.Request) {
	var req model.ResourceCommonReq
	if err := r.ParseForm(&req); err != nil {
		response.JsonExitWithMsg(r, define.Fail, err.Error())
	}

	if _, err := dao.MyadminResource.Where(dao.MyadminResource.Columns.Id, req.Id).Delete(); err != nil {
		defErr.Panic(define.DataOptFail)
	}
	response.JsonSuc(r)
}

// @summary 资源获取父节点
// @tags 设置
// @produce json
// @param   id formData  int false "ID"
// @router  /resource/get-plist [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *resourceMgrApi) GetPlist(r *ghttp.Request) {
	var req model.ResourceCommonReq
	if err := r.ParseForm(&req); err != nil {
		response.JsonExitWithMsg(r, define.Fail, err.Error())
	}
	response.JsonSuc(r, service.Resource.GetParentResourceList(req.Id))
}
